package com.steven.ourcontest.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.fragment.app.DialogFragment

/**
 * 这个show出来后 上一个关闭
 */
abstract class BaseNextDialogFragment : DialogFragment() {

    private var mNextDialog: BaseNextDialogFragment? = null
    private var mCloseLastListener: (() -> Unit)? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(getLayoutResId(), container, false)
    }

    @LayoutRes
    abstract fun getLayoutResId(): Int

    override fun onResume() {
        super.onResume()
        mCloseLastListener?.invoke()
        mCloseLastListener = null
    }

    fun setNextDialog(dialog: BaseNextDialogFragment): BaseNextDialogFragment {
        mNextDialog = dialog
        mNextDialog?.setCloseThisListener {
            if (isVisible) {
                dismissAllowingStateLoss()
            }
        }
        return this
    }

    private fun setCloseThisListener(listener: (() -> Unit)): BaseNextDialogFragment {
        mCloseLastListener = listener
        return this
    }
}